﻿using System;
using System.Collections.ObjectModel;
using System.Globalization;

namespace Silverlight
{
    /// <summary>
    /// Provides DataFieldCollection functionality
    /// </summary>
    public class DataFieldCollection : Collection<DataField>
    {
        #region Constructors
        /// <summary>
        /// Default constructor
        /// </summary>
        internal DataFieldCollection()
        {
        }
        #endregion

        #region Indexers
        /// <summary>
        /// Gets DataField with specified name
        /// </summary>
        /// <param name="name">DataField name</param>
        /// <returns>DataField if found, otherwise null</returns>
        public DataField this[string name]
        {
            get
            {
                // Validate argument
                if (string.IsNullOrEmpty(name))
                {
                    throw new ArgumentNullException("name");
                }

                // Do case insensitive search
                string caseInsensitiveName = name.ToUpper(CultureInfo.InvariantCulture);
                foreach (DataField dataField in this)
                {
                    if (dataField.ColumnName.ToUpper(CultureInfo.InvariantCulture).Equals(caseInsensitiveName))
                    {
                        return dataField;
                    }
                }

                return null;
            }
        }
        #endregion
    }
}
